<?php

namespace App\Http\Controllers\Api;

use App\Model\Cate;
use App\Model\Goods;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
     * 分类初始化数据接口
     */
    public function index (){
        $cate = new Cate();
        $goods = new Goods();
        $cateInfo = $cate->select('cate.id','cate.name','goods.reprice')->leftJoin('goods','cate_id','=','cate.id')->where('cate.id','>','1')->where('goods.name','!=',null)->where('goods.status','1')->groupBy('cate_id')->get();
        $goodsInfo = $goods->where('cate_id','=',$cateInfo[0]['id'])->where('status','=','1')->orderBy('num','desc')->get();
        if(count($cateInfo)>0){
            $this->success(array('cateInfo'=>$cateInfo,'goodsInfo'=>$goodsInfo));
        }else{
            $this->error('暂无分类信息');
        }
    }

    /**
     * @param Request $request
     * 分类商品接口
     */
    public function goodsInfo(Request $request){
        $goods = new Goods();
        $id = $request->input("cate_id");
        $goodsInfo = $goods->where('cate_id','=',$id)->where('status','=','1')->orderBy('num','desc')->get();
        if(count($goodsInfo)>0){
            $this->success($goodsInfo);
        }else{
            $this->error('该分类暂无商品'.__LINE__);
        }
    }
}
